package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.CarManageBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberCheckStateEntity;
import com.sgb.member.entity.MemberLogisticsEntity;
import com.sgb.member.ui.fragment.BusinessLogisticsFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BusinessLogisticsMode3 extends BaseViewModel {

    private final BusinessLogisticsFragment3 fragment;
    private final MemberRepertory mRepertory;
    private final int maxPageSize = 10;
    private int current;

    private CommonPopEntity mAll;

    public BusinessLogisticsMode3(BusinessLogisticsFragment3 fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    // 物流货物
    public void getTabType() {
        mRepertory.getApi().findCarTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, new CommonPopEntity("", "全部"));
                    fragment.setTypeDatas(data.getData());
                } else {
                    fragment.showEmpty();
                }
            }
        });
    }

    //根据类型获取车长
    public void getCarTypeAndLen(String no) {
        mRepertory.getApi().getCarTypeAndLen(no).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    fragment.setCarTypeAndLen(data.getData(), 0);
                }
            }
        });
    }

    public void getAllCarLen() {
        mRepertory.getApi().carLenByCarTypeIndex(new ArrayList<>()).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    fragment.setCarTypeAndLen(data.getData(), 1);
                }
            }
        });
    }

    //获取全部车辆功能
    public void carFunctionConfig() {
        mRepertory.getApi().carFunctionConfig().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0,new CommonPopEntity("","全部"));
                    fragment.setCarFunctionConfig(data.getData());
                }
            }
        });
    }


    public void getFilterSourceList(boolean isLoadMore, String goodsModeNo, String mNo1,
                                    String mNo2, String cart_length, String cart_height,
                                    String cart_weight) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        map.put("limit", maxPageSize);
        ArrayList<Object> carTypeNos = new ArrayList<>();
        if(!TextUtils.isEmpty(goodsModeNo)){
            carTypeNos.add(goodsModeNo);
        }
        map.put("carTypeNos", carTypeNos);//车型编号 list

        ArrayList<Object> cart_length_list = new ArrayList<>();
        if(!TextUtils.isEmpty(cart_length)){
            cart_length_list.add(cart_length);
        }
        map.put("carLenNos", cart_length_list); // 	车长编号 list

        extractedHeightVolume(cart_height,map);//设置栏高
        extractedWeigetVolume(cart_weight,map);//载重
        map.put("functionNo", mNo1);//功能
        map.put("functionNo", mNo1);
        extractedYearVolume(mNo2,map);//年限

        map.put("areacode", mRepertory.getAreaCode());
        map.put("isApp", 1);
        showOrHideDialog(true);
        mRepertory.getApi().mangeMentCarsList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<CarManageBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<CarManageBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<CarManageBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }
        });
    }

    //栏高
    public void getLanHeightVolume() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("0", "0-0.5米"));
        list.add(new CommonPopEntity("1", "0.5-1米"));
        list.add(new CommonPopEntity("2", "1-1.5米"));
        list.add(new CommonPopEntity("3", "1.5-2米"));
        list.add(new CommonPopEntity("4", "2-3米"));
        list.add(new CommonPopEntity("5", "3米以上"));
        fragment.setVolumeData(list);
    }

    private void extractedHeightVolume(String volumeNo, Map<String, Object> map) {
        int volume = TextUtils.isEmpty(volumeNo) ? -1 : Integer.parseInt(volumeNo);
        switch (volume) {
            case 0:
                map.put("minHeight", 0);
                map.put("maxHeight", 0.5);
                break;
            case 1:
                map.put("minHeight", 0.5);
                map.put("maxHeight", 1);
                break;
            case 2:
                map.put("minHeight", 1);
                map.put("maxHeight", 1.5);
                break;
            case 3:
                map.put("minHeight", 1.5);
                map.put("maxHeight", 2);
                break;
            case 4:
                map.put("minHeight", 2);
                map.put("maxHeight", 3);
                break;
            case 5:
                map.put("minHeight", 3);
                map.put("maxHeight", "");
                break;
            default:
                map.put("minHeight", "");
                map.put("maxHeight", "");
                break;
        }
    }

    private void extractedWeigetVolume(String volumeNo, Map<String, Object> map) {
        int volume = TextUtils.isEmpty(volumeNo) ? -1 : Integer.parseInt(volumeNo);
        switch (volume) {
            case 0:
                map.put("minWeight", 0);
                map.put("maxWeight", 5);
                break;
            case 1:
                map.put("minWeight", 5);
                map.put("maxWeight", 10);
                break;
            case 2:
                map.put("minWeight", 10);
                map.put("maxWeight", 20);
                break;
            case 3:
                map.put("minWeight", 20);
                map.put("maxWeight", 30);
                break;
            case 4:
                map.put("minWeight", 30);
                map.put("maxWeight", 40);
                break;
            case 5:
                map.put("minWeight", 40);
                map.put("maxWeight", "");
                break;
            default:
                map.put("minWeight", "");
                map.put("maxWeight", "");
                break;
        }
    }

    private void extractedYearVolume(String volumeNo, Map<String, Object> map) {
        int volume = TextUtils.isEmpty(volumeNo) ? -1 : Integer.parseInt(volumeNo);
        switch (volume) {
            case 0:
                map.put("minCarYear", 0);
                map.put("maxCarYear", 2);
                break;
            case 1:
                map.put("minCarYear", 3);
                map.put("maxCarYear", 5);
                break;
            case 2:
                map.put("minCarYear", 6);
                map.put("maxCarYear", 8);
                break;
            case 3:
                map.put("minCarYear", 8);
                map.put("maxCarYear", 10);
                break;
            case 4:
                map.put("minCarYear", 10);
                map.put("maxCarYear", "");
                break;
            default:
                map.put("minCarYear", "");
                map.put("maxCarYear", "");
                break;
        }
    }

    public void getWeight() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("0", "0-5吨"));
        list.add(new CommonPopEntity("1", "5-10吨"));
        list.add(new CommonPopEntity("2", "10-20吨"));
        list.add(new CommonPopEntity("3", "20-30吨"));
        list.add(new CommonPopEntity("4", "30-40吨"));
        list.add(new CommonPopEntity("5", "40吨以上"));
        fragment.setWeightData(list);
    }

    public void getYear() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("0", "1-2年"));
        list.add(new CommonPopEntity("1", "3-5年"));
        list.add(new CommonPopEntity("2", "6-8年"));
        list.add(new CommonPopEntity("3", "8-10年"));
        list.add(new CommonPopEntity("4", "10年以上"));
        fragment.setYearData(list);
    }


}
